package com.accloud.cloudservice;

import com.accloud.common.ACConfiguration;
import com.accloud.service.ACException;
import com.accloud.service.ACMsg;
import com.accloud.service.ACObject;
import com.accloud.service.ACUserCommand;
import com.accloud.service.ACUserGroup;
import com.accloud.service.ACUserTask;
import com.accloud.service.ACUserTimerMgr;
import com.accloud.utils.PreferencesUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

public class ACUserTimerManager extends ACBaseManager implements ACUserTimerMgr {
    private TimeZone timeZone;

    public ACUserTimerManager() {
        this(TimeZone.getDefault());
    }

    public ACUserTimerManager(TimeZone timeZone2) {
        super(ACConfiguration.TIMER_SERVICE_NAME, 1, false);
        this.timeZone = timeZone2;
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void addTask(ACUserTask task, PayloadCallback<ACUserTask> callback) {
        final PayloadCallback<ACUserTask> callbackWrapper = new PayloadEventCallback<>(new ACTaskEvent("create", String.valueOf(task.getTaskId()), "uds", task.getName(), "single"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("addCloudTask");
        req.put("task", getObjFromTask(task));
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACUserTimerManager.AnonymousClass1 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                try {
                    ACObjectMarshaller.unmarshal(resp, resp.getPayload());
                    callbackWrapper.success(ACUserTimerManager.this.getTaskFromObj((ACObject) resp.get("task")));
                } catch (IOException e) {
                    callbackWrapper.error(new ACException(ACException.MARSHAL_ERROR, e.toString()));
                }
            }
        });
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void modifyTask(ACUserTask task, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("modify", String.valueOf(task.getTaskId()), "uds", task.getName(), "single"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("updateCloudTask");
        req.put("taskId", Long.valueOf(task.getTaskId()));
        req.put("task", getObjFromTask(task));
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void openTask(long taskId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("start", String.valueOf(taskId), "uds", null, "single"), callback);
        if (taskId <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("enableCloudTask");
            req.put("taskId", Long.valueOf(taskId));
            try {
                req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
            } catch (IOException e) {
            }
            sendReq(req, callbackWrapper);
        }
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void closeTask(long taskId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("stop", String.valueOf(taskId), "uds", null, "single"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("disableCloudTask");
        req.put("taskId", Long.valueOf(taskId));
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void deleteTask(long taskId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("delete", String.valueOf(taskId), "uds", null, "single"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("delCloudTask");
        req.put("taskId", Long.valueOf(taskId));
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void listTasks(final PayloadCallback<List<ACUserTask>> callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("listCloudTasksByOwner");
        req.put("ownerType", 2);
        req.put("ownerId", Long.valueOf(PreferencesUtils.getLong(AC.context, ACConfiguration.KEY_USERID, 0)));
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACUserTimerManager.AnonymousClass2 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                try {
                    ACObjectMarshaller.unmarshal(resp, resp.getPayload());
                    callback.success(ACUserTimerManager.this.getTasksFromObjs((List) resp.get("tasks")));
                } catch (IOException e) {
                    callback.error(new ACException(ACException.MARSHAL_ERROR, e.toString()));
                }
            }
        });
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void addTaskGroup(ACUserGroup group, PayloadCallback<ACUserGroup> callback) {
        final PayloadCallback<ACUserGroup> callbackWrapper = new PayloadEventCallback<>(new ACTaskEvent("delete", group.getGroupId(), "uds", group.getGroupName(), "group"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else if (group == null || group.getTasks() == null || group.getTasks().size() <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("addTaskGroup");
            req.put("tasks", getObjsFromTasks(group.getTasks()));
            req.put("groupName", group.getGroupName());
            try {
                req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
            } catch (IOException e) {
            }
            sendReq(req, callbackWrapper, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACUserTimerManager.AnonymousClass3 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    try {
                        ACObjectMarshaller.unmarshal(resp, resp.getPayload());
                    } catch (IOException e) {
                    }
                    ACObject object = (ACObject) resp.get("group");
                    callbackWrapper.success(new ACUserGroup((String) object.get("id"), (String) object.get("name"), ACUserTimerManager.this.getTasksFromObjs((List) object.get("tasks"))));
                }
            });
        }
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void modifyTaskGroup(ACUserGroup group, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("modify", group.getGroupId(), "uds", group.getGroupName(), "group"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else if (group == null || group.getGroupId() == null || group.getTasks() == null || group.getTasks().size() <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("updateTaskGroup");
            req.put("groupId", group.getGroupId());
            req.put("tasks", getObjsFromTasks(group.getTasks()));
            req.put("groupName", group.getGroupName());
            try {
                req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
            } catch (IOException e) {
            }
            sendReq(req, callbackWrapper);
        }
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void listTaskGroups(final PayloadCallback<List<ACUserGroup>> callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("listTaskGroups");
        req.put("ownerId", Long.valueOf(PreferencesUtils.getLong(AC.context, ACConfiguration.KEY_USERID, 0)));
        req.put("ownerType", 2);
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACUserTimerManager.AnonymousClass4 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                try {
                    ACObjectMarshaller.unmarshal(resp, resp.getPayload());
                } catch (IOException e) {
                }
                List<ACObject> objects = (List) resp.get("groups");
                ArrayList arrayList = new ArrayList();
                if (objects != null) {
                    for (ACObject object : objects) {
                        arrayList.add(new ACUserGroup((String) object.get("id"), (String) object.get("name"), ACUserTimerManager.this.getTasksFromObjs((List) object.get("tasks"))));
                    }
                }
                callback.success(arrayList);
            }
        });
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void getTaskGroup(String groupId, final PayloadCallback<ACUserGroup> callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("getTaskGroup");
        req.put("groupId", groupId);
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACUserTimerManager.AnonymousClass5 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                try {
                    ACObjectMarshaller.unmarshal(resp, resp.getPayload());
                } catch (IOException e) {
                }
                ACObject object = (ACObject) resp.get("group");
                callback.success(new ACUserGroup((String) object.get("id"), (String) object.get("name"), ACUserTimerManager.this.getTasksFromObjs((List) object.get("tasks"))));
            }
        });
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void deleteTaskGroup(String groupId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("delete", groupId, "uds", null, "group"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("delTaskGroup");
        req.put("groupId", groupId);
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void openTaskGroup(String groupId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("start", groupId, "uds", null, "group"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("enableTaskGroup");
        req.put("groupId", groupId);
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    @Override // com.accloud.service.ACUserTimerMgr
    public void closeTaskGroup(String groupId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("start", groupId, "uds", null, "group"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("disableTaskGroup");
        req.put("groupId", groupId);
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    private ACObject getObjFromTask(ACUserTask task) {
        ACObject object = new ACObject();
        object.put("name", task.getName());
        object.put("desc", task.getDescription());
        ACObject timeRule = new ACObject();
        if (task.getTimeZone() != null) {
            timeRule.put("timeZone", task.getTimeZone());
        } else {
            timeRule.put("timeZone", this.timeZone.getID());
        }
        timeRule.put("timeCycle", task.getTimeCycle());
        timeRule.put("timePoint", task.getTimePoint());
        object.put("timeRule", timeRule);
        ACObject command = new ACObject();
        command.put("name", "uds-command");
        ACObject UDSCommand = new ACObject();
        if (task.getUserCommand().getSubDomain() != null) {
            UDSCommand.put("subDomain", task.getUserCommand().getSubDomain());
        }
        UDSCommand.put("service", task.getUserCommand().getServiceName());
        UDSCommand.put("method", task.getUserCommand().getReq().getName());
        UDSCommand.put("params", task.getUserCommand().getReq());
        command.put("udsCmd", UDSCommand);
        object.put("command", command);
        return object;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private ACUserTask getTaskFromObj(ACObject object) {
        ACUserTask task = new ACUserTask();
        long id = ((Long) object.get("id")).longValue();
        long status = object.getLong("status");
        task.setTaskId(id);
        task.setName((String) object.get("name"));
        task.setDescription((String) object.get("desc"));
        task.setStatus((int) status);
        ACObject timeRule = (ACObject) object.get("timeRule");
        task.setTimeZone((String) timeRule.get("timeZone"));
        task.setTimeCycle((String) timeRule.get("timeCycle"));
        task.setTimePoint((String) timeRule.get("timePoint"));
        ACObject command = (ACObject) object.get("command");
        if (((String) command.get("name")).equals("uds-command")) {
            ACObject udsCmd = (ACObject) command.get("udsCmd");
            ACMsg req = new ACMsg();
            req.setName((String) udsCmd.get("method"));
            req.setObjectData(((ACObject) udsCmd.get("params")).getObjectData());
            task.setUserCommand(new ACUserCommand((String) udsCmd.get("subDomain"), (String) udsCmd.get("service"), req));
        }
        task.setCreateTime(object.getString("createTime"));
        task.setModifyTime(object.getString("modifyTime"));
        return task;
    }

    private List<ACObject> getObjsFromTasks(List<ACUserTask> tasks) {
        List<ACObject> taskObjs = new ArrayList<>();
        for (ACUserTask task : tasks) {
            ACObject object = new ACObject();
            object.put("name", task.getName());
            if (task.getDescription() != null) {
                object.put("desc", task.getDescription());
            }
            ACObject timeRule = new ACObject();
            if (task.getTimeZone() == null) {
                timeRule.put("timeZone", this.timeZone.getID());
            } else {
                timeRule.put("timeZone", task.getTimeZone());
            }
            timeRule.put("timeCycle", task.getTimeCycle());
            timeRule.put("timePoint", task.getTimePoint());
            object.put("timeRule", timeRule);
            ACObject command = new ACObject();
            command.put("name", "uds-command");
            ACObject UDSCommand = new ACObject();
            if (task.getUserCommand().getSubDomain() != null) {
                UDSCommand.put("subDomain", task.getUserCommand().getSubDomain());
            }
            UDSCommand.put("service", task.getUserCommand().getServiceName());
            UDSCommand.put("method", task.getUserCommand().getReq().getName());
            UDSCommand.put("params", task.getUserCommand().getReq());
            command.put("udsCmd", UDSCommand);
            object.put("command", command);
            taskObjs.add(object);
        }
        return taskObjs;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private List<ACUserTask> getTasksFromObjs(List<ACObject> objects) {
        List<ACUserTask> tasks = new ArrayList<>();
        if (objects != null) {
            for (ACObject object : objects) {
                ACUserTask task = new ACUserTask();
                long id = ((Long) object.get("id")).longValue();
                long status = ((Long) object.get("status")).longValue();
                ACObject timeRule = (ACObject) object.get("timeRule");
                task.setTaskId(id);
                task.setName((String) object.get("name"));
                task.setDescription((String) object.get("desc"));
                task.setTimeZone((String) timeRule.get("timeZone"));
                task.setTimeCycle((String) timeRule.get("timeCycle"));
                task.setTimePoint((String) timeRule.get("timePoint"));
                task.setStatus((int) status);
                ACObject command = (ACObject) object.get("command");
                if (((String) command.get("name")).equals("uds-command")) {
                    ACObject udsCmd = (ACObject) command.get("udsCmd");
                    ACMsg req = new ACMsg();
                    req.setName((String) udsCmd.get("method"));
                    req.setObjectData(((ACObject) udsCmd.get("params")).getObjectData());
                    task.setUserCommand(new ACUserCommand((String) udsCmd.get("subDomain"), (String) udsCmd.get("service"), req));
                }
                task.setCreateTime(object.getString("createTime"));
                task.setModifyTime(object.getString("modifyTime"));
                tasks.add(task);
            }
        }
        return tasks;
    }
}
